package org.szgb.console.ht.meter.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.szgb.console.ht.meter.entity.HtMeterReadingDaily;
import org.szgb.console.ht.meter.mapper.HtMeterReadingDailyMapper;
import org.szgb.console.sys.todo.entity.SysTodo;
import org.szgb.console.sys.todo.service.SysTodoService;
import org.szgb.console.sys.user.entity.SysUser;
import org.szgb.console.sys.user.mapper.SysUserMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;
import org.szgb.console.wecom.robot.service.WeComRobotService;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
public class HtMeterReadingDailyService {
    private final HtMeterReadingDailyMapper htMeterReadingDailyMapper;
    private final SysUserMapper sysUserMapper;

    private final SysTodoService sysTodoService;
    private final WeComRobotService weComRobotService;

    @Autowired
    public HtMeterReadingDailyService(HtMeterReadingDailyMapper htMeterReadingDailyMapper, SysTodoService sysTodoService, SysUserMapper sysUserMapper, ContentNegotiatingViewResolver contentNegotiatingViewResolver,WeComRobotService weComRobotService){
        this.htMeterReadingDailyMapper = htMeterReadingDailyMapper;
        this.sysTodoService = sysTodoService;
        this.sysUserMapper = sysUserMapper;
        this.weComRobotService = weComRobotService;
    }

    @Transactional(readOnly = true)
    public HtMeterReadingDaily get(String id) {
        return htMeterReadingDailyMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<HtMeterReadingDaily> findList(HtMeterReadingDaily htMeterReadingDaily) {
        if (htMeterReadingDaily != null && htMeterReadingDaily.getPageDto() != null) {
            Page<HtMeterReadingDaily> page = new Page<>(htMeterReadingDaily.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            htMeterReadingDaily.setPage(page);
        }
        return htMeterReadingDailyMapper.findList(htMeterReadingDaily);
    }

    @Transactional(readOnly = true)
    public Integer getCount(HtMeterReadingDaily htMeterReadingDaily) {
        return htMeterReadingDailyMapper.getCount(htMeterReadingDaily);
    }

    @Transactional()
    public void save(HtMeterReadingDaily htMeterReadingDaily) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (htMeterReadingDaily.getId() == null || htMeterReadingDaily.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            htMeterReadingDaily.setCreatedBy(htMeterReadingDaily.getUpdatedBy());
            // 添加UUID
            htMeterReadingDaily.setId(UUIDUtil.getUUID());
            // 设置审批为false
            htMeterReadingDaily.setIsApproval(false);
            // 执行插入操作
            htMeterReadingDailyMapper.insert(htMeterReadingDaily);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            htMeterReadingDailyMapper.update(htMeterReadingDaily);
        }
    }

    @Transactional()
    public void insertBatch(List<HtMeterReadingDaily> htMeterReadingDailyList) throws IOException, InterruptedException {
        SysUser user = sysUserMapper.get(htMeterReadingDailyList.getFirst().getCreatedBy().getId());
        List<SysUser> userList = sysUserMapper.getUserInDepartmentWithRole(user.getDepartment().getId(), "48118353-c5d1-46ec-bb83-0e002c087a91");
        for (HtMeterReadingDaily htMeterReadingDaily : htMeterReadingDailyList) {
            htMeterReadingDaily.setId(UUIDUtil.getUUID());
            CompletableFuture<Void> future = CompletableFuture.runAsync(()->{
                String title = "电表抄表";
                String content = "请审批电表抄表";
                sysTodoService.asyncCreateTodo(9, 1, userList.stream().map(SysUser::getId).toArray(String[]::new), htMeterReadingDaily.getId(), title, content);
            });
        }
        weComRobotService.sendText("电表抄表" + "   " + "请审批电表抄表", userList.stream().map(SysUser::getUsername).toArray(String[]::new));

        htMeterReadingDailyMapper.insertBatch(htMeterReadingDailyList);
    }

    @Transactional
    public void delete(String ids) {
        htMeterReadingDailyMapper.delete(ids.split(","));
    }

    @Transactional
    public void approval(HtMeterReadingDaily htMeterReadingDaily) {
        sysTodoService.approval(htMeterReadingDaily.getId(),htMeterReadingDaily.getUpdatedBy().getId());
        htMeterReadingDailyMapper.update(htMeterReadingDaily);
    }
}
